<template>
	<view>
		<cu-custom bgColor="bg-black" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">委托详情</block>
		</cu-custom>
		<view class="login_top">
			<view class="cu-avatar xl round margin-left" style="background-image:url(https://ossweb-img.qq.com/images/lol/web201310/skin/big99008.jpg);"></view>
			<view class="top_title">
				全世界都在用的律师小程序
			</view>
		</view>
		<view class="my_login_box">
			<view class="login_title">
				登录成功后可继续当前操作
			</view>
			<button open-type="getUserInfo" class="cu-btn block bg-blue margin-tb-sm lg my_login_btn" @click="loginMP">
				<text class="cuIcon-weixin login_weixin"></text>微信快捷登录</button>
		</view>
	</view>
</template>

<script>
	import {
		login,
		checkToken
	} from '../../core/login.js'
	export default {
		data() {
			return {
				code: '',
				token: ''
			};
		},
		methods: {
			async loginMP() {
				let _this = this

				console.log(checkToken(), 'checkToken()')
				if (checkToken()) {
					uni.getUserInfo({
						success: (res3) => {
							console.log(res3, '授权成功',uni.getStorageSync('AUTH_TOKEN'));
							uni.request({
								url: `http://172.16.15.135:2020/wx/user/wxf60bdde4472ae369/info`,
								method: 'get',
								header: {
									token: uni.getStorageSync('AUTH_TOKEN')
								},
								data: {
								  encryptedData:res3.encryptedData,
								  iv:res3.iv,
								},
								success: res4 => {
									console.log('返回', res4.data);
									// uni.showModal({
									// 	content:res4.data.data,
									// 	title:res4.data.data
									// })
									uni.showToast({
										title:res4.data.data.nickName
									})
								}
							})
						},
						fail: () => {
							console.log("未授权");
						}
					})
				} else {
					let res = await login()
				}

				// console.log(res)
				// uni.login({
				// 	provider: 'weixin',
				// 	success: function(res) {
				// 		console.log(res.code);
				// 		_this.code = res.code
				// 		uni.request({
				// 			url: 'http://172.16.15.135:8100/wechatMini/shop/wxUser/wx6b469b07ef4a6699/login?code', //仅为示例，并非真实接口地址。
				// 			method: 'get',
				// 			success: res2 => {
				// 				console.log('登录', res2.data);
				// 				_this.token = res2.data.data
				// 				uni.getUserInfo({
				// 					success: (res3) => {
				// 						console.log(res3,'授权成功');
				// 						uni.request({
				// 							url: `http://172.16.15.135:8100/wechatMini/shop/wxUser/wx6b469b07ef4a6699/getUserInfo?encryptedData=${res3.encryptedData}&ivStr=${res3.iv}`, 
				// 							method: 'get',
				// 							header:{
				// 								token:res2.data.data
				// 							},
				// 							success: res4 => {
				// 								console.log('返回', res4.data);
				// 							}
				// 						})
				// 					},
				// 					fail: () => {
				// 						console.log("未授权");
				// 					}
				// 				})

				// 			}
				// 		})
				// 	}
				// });

			},
			getUserInfo() {
				let _this = this
				uni.getUserInfo({
					provider: 'weixin',
					withCredentials: true,
					success: (res) => {
						console.log(res, '授权成功');
						uni.request({
							url: `http://172.16.15.135:2020/wx/user/wxf60bdde4472ae369/info?encryptedData=${res.encryptedData}&iv=${res.iv}`,
							method: 'get',
							header: {
								token: _this.token
							},
							success: res => {
								console.log('返回', res.data);
							}
						})
					},
					fail: () => {
						console.log("未授权");
					}
				})
			}

		}
	}
</script>

<style lang="scss">
	.login_top {
		margin-top: 50upx;
		text-align: center;

		.top_title {
			margin: 20upx 0;
		}
	}

	.my_login_box {
		position: fixed;
		bottom: 100upx;
		width: 100%;
		text-align: center;
	}

	.my_login_btn {
		width: 60%;
		margin: 0 auto;

		.login_weixin {
			font-size: 50upx;
			margin-right: 16upx;
		}
	}

	.login_title {
		margin: 20upx 0;

	}
</style>
